<template>
  <div class="app-container">
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['prod:category:add']"
        >新增一级类目
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="info"
          plain
          icon="el-icon-sort"
          size="mini"
          @click="toggleExpandAll"
        >展开/折叠
        </el-button>
      </el-col>
      <!--      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>-->
    </el-row>

    <el-table
      v-if="refreshTable"
      v-loading="loading"
      :data="categoryList"
      style="width: 100%;margin-bottom: 50px;"
      row-key="id"
      :default-expand-all="isExpandAll"
      :tree-props="{children: 'children', hasChildren: 'hasChildren'}"
    >
      <el-table-column prop="name" label="名称" width="260"></el-table-column>
      <el-table-column prop="description" label="描述" width="200"></el-table-column>
      <el-table-column
        prop="status"
        label="状态"
        align="center">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.common_data_status" :value="scope.row.status"/>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">

          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            v-hasPermi="['prod:category:edit']"
            v-if="scope.row.level == 3"
            @click="handleBrand(scope.row)"
          >品牌关联
          </el-button>

          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            v-hasPermi="['prod:category:edit']"
            v-if="scope.row.level == 3"
            @click="associationProperties(scope.row)"
          >属性关联
          </el-button>

          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            v-hasPermi="['prod:category:edit']"
            v-if="scope.row.level == 3"
            @click="associationParameters(scope.row)"
          >参数关联
          </el-button>

          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            v-hasPermi="['prod:category:edit']"
            @click="handleEdit(scope.row)"
          >修改
          </el-button>

          <el-button
            v-if="scope.row.level !== 3"
            size="mini"
            type="text"
            icon="el-icon-plus"
            v-hasPermi="['prod:category:add']"
            @click="handleAdd(scope.row)"
          >新增
          </el-button>

          <el-button
            v-if="scope.row.children.length === 0"
            size="mini"
            type="text"
            icon="el-icon-delete"
            v-hasPermi="['prod:category:remove']"
            @click="handleDelete(scope.row)"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 添加或修改分类对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="600px" append-to-body>
      <el-form ref="dataForm" :model="dataForm" :rules="validateForm" label-width="80px">
        <el-form-item label="名称" prop="name">
          <el-input v-model="dataForm.name" placeholder="请输入分类" ref="dataFormNameRef"/>
        </el-form-item>
        <el-form-item label="描述" prop="description">
          <el-input v-model="dataForm.description" placeholder="请输入描述"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="open = false">取 消</el-button>
      </div>
    </el-dialog>

    <brand-association ref="associationBrandRef" :categoryId = "categoryId" ></brand-association>
    <attribute-association ref="attributeAssociationRef"></attribute-association>
    <parameter-association ref="parameterAssociationRef"></parameter-association>
  </div>
</template>

<script>

import {
  getCategoryList,
  addCategory,
  getCategory,
  delCategory,
  updateCategory,
} from "@/api/prod/category";

import brandAssociation from "@/views/components/brandAssociation";
import attributeAssociation from "@/views/components/attributeAssociation";
import parameterAssociation from "@/views/components/parameterAssociation";


export default {
  name: "Category",
  components: {attributeAssociation, brandAssociation, parameterAssociation},
  dicts: ['common_data_status'],
  data() {
    return {
      categoryList: [],
      // 是否展开，默认全部展开
      isExpandAll: true,
      // 重新渲染表格状态
      refreshTable: true,
      loading: false,
      open: false,
      title: "",
      dataForm: {
        id: undefined,
        parentId: undefined,
        level: 1,
        name: undefined,
        description: undefined
      },
      validateForm: {
        name: [
          {required: true, message: "名称不能为空", trigger: "blur"}
        ]
      },

      //当前分类id
      categoryId: 0
    };
  },
  created() {
    this.getCategoryList();
  },
  methods: {
    /** 查询类目列表 */
    getCategoryList() {
      this.loading = true;
      getCategoryList().then(response => {
        this.categoryList = this.treeStructure(response.data);
        this.loading = false;
      })
    },

    resetDataForm() {
      this.dataForm = {
        id: undefined,
        parentId: undefined,
        level: "",
        name: undefined,
        description: undefined
      };
      this.resetForm("dataForm");
    },

    handleAdd(row) {
      this.resetDataForm();
      this.open = true;
      this.title = "新增类名";
      this.focus("dataFormNameRef")
      if (row.level) {
        this.dataForm.level = row.level + 1;
      } else {
        this.dataForm.level = 1;
      }
      this.dataForm.parentId = row.id || 0;
    },
    handleEdit(row) {
      this.resetDataForm();
      getCategory(row.id).then(response => {
        this.dataForm.id = response.data.id
        this.dataForm.parentId = response.data.parentId
        this.dataForm.level = response.data.level
        this.dataForm.name = response.data.name
        this.dataForm.description = response.data.description
        this.open = true;
        this.title = "修改分类"
        this.focus("dataFormNameRef")
      })
    },

    handleDelete(row) {
      const id = row.id
      this.$modal.confirm('是否确认删除ID为"' + id + '"的数据项？').then(function () {
        return delCategory(id);
      }).then(() => {
        this.getCategoryList();
        this.$modal.msgSuccess("删除成功");
      })
    },

    /** 展开/折叠操作 */
    toggleExpandAll() {
      this.refreshTable = false;
      this.isExpandAll = !this.isExpandAll;
      this.$nextTick(() => {
        this.refreshTable = true;
      });
    },

    submitForm() {
      this.$refs.dataForm.validate(valid => {
        if (valid) {
          // if (this.dataForm.id !== undefined && this.dataForm.id !== null && this.dataForm.id !== 0) {
          if (this.dataForm.id) {
            updateCategory(this.dataForm).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getCategoryList();
            })
          } else {
            addCategory(this.dataForm).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getCategoryList();
            })
          }
        }
      })
    },

    handleBrand(row) {
      this.categoryId = row.id
      this.$refs.associationBrandRef.show();
    },

    associationProperties(row){
      // this.categoryId = row.id
      this.$refs.attributeAssociationRef.show(row.id);
    },

    associationParameters(row){
      this.$refs.parameterAssociationRef.show(row.id);
    }
  },

};
</script>

<style>

</style>



